package imageclipper

import (
	"errors"
	"fmt"
	"log"
	"os"
	"path"
)

/*
裁剪前清理
	1. 将超目标裁剪区域以外的图片删除
	2. 将属于目标裁剪区域的图片进行去边化处理
	params:
		file_path:     文件路径
		extension:     图片扩展名
		cols, rows:    裁剪前的列数、行数
		x, y:          从左上角坐标开始裁剪
		width, height: 裁剪区域宽、高
*/
func clear(file_path, extension string, cols, rows, x, y, width, height int) (err error) {
	x2 := x + width
	y2 := y + height
	for c := 0; c < cols; c++ {
		for r := 0; r < rows; r++ {

			// 当前图片坐标
			left := c * tileWidth
			top := r * tileHeight
			right := left + tileWidth
			bottom := top + tileHeight

			// 如果当前图片超出区域，直接删除
			if bottom <= y || right <= x || top >= y2 || left >= x2 {
				// log.Println("left:", left, "top:", top, "right:", right, "bottom:", bottom, "x:", x, "y:", y, "x2:", x2, "y2:", y2)
				// if bottom <= x {
				// 	log.Println("bottom <= y", bottom, y)
				// }
				// if right <= y {
				// 	log.Println("right <= x", right, x)
				// }
				// if left >= x2 {
				// 	log.Println("left >= x2", left, x2)
				// }
				// if top >= y2 {
				// 	log.Println("top >= y2", top, y2)
				// }
				img_path := path.Join(file_path, fmt.Sprintf("%d_%d.%s", c, r, extension))
				log.Println("删除:", img_path)
				if err := os.Remove(img_path); err != nil {
					return err
				}
				continue
			}

			// 如果当前图片属于区域内，去边存储
			if x > left {
				left = x - left
			} else {
				left = 0
			}

			if y > top {
				top = y - top
			} else {
				top = 0
			}

			if right > x2 {
				right = tileWidth - (right - x2)
			} else {
				right = tileWidth
			}

			if bottom > y2 {
				bottom = tileHeight - (bottom - y2)
			} else {
				bottom = tileHeight
			}

			img_path := path.Join(file_path, fmt.Sprintf("%d_%d.%s", c, r, extension))

			// 如果图片大小不需要改变 直接跳过
			if left == 0 && right == tileWidth && top == 0 && bottom == tileHeight {
				log.Println("跳过:", img_path)
				continue
			}

			log.Println("抠图:", img_path)
			if err := cut(img_path, left, top, right, bottom); err != nil {
				return err
			}
		}
	}
	return nil
}

/*
重排文件名
	1. 依照 0_0 为起始名称重新排序
	params:
		file_path:   文件路径
		extension:   图片扩展名
		cols, rows:  裁剪前列数、行数
	returns:
		err: error
*/
func renames(file_path, extension string, cols, rows int) (err error) {
	// 改 row
	for c := 0; c < cols; c++ {
		rr := 0
		for r := 0; r < rows; r++ {
			old_path := path.Join(file_path, fmt.Sprintf("%d_%d.%s", c, r, extension))
			new_path := path.Join(file_path, fmt.Sprintf("%d_%d.%s", c, rr, extension))
			fi, err := os.Stat(old_path)
			if err != nil {
				continue
			}
			if fi.IsDir() {
				return errors.New("不是有效文件")
			}
			if err := os.Rename(old_path, new_path); err != nil {
				return err
			}
			rr++
		}
	}
	// 改 col
	for r := 0; r < rows; r++ {
		cc := 0
		for c := 0; c < cols; c++ {
			old_path := path.Join(file_path, fmt.Sprintf("%d_%d.%s", c, r, extension))
			new_path := path.Join(file_path, fmt.Sprintf("%d_%d.%s", cc, r, extension))
			fi, err := os.Stat(old_path)
			if err != nil {
				continue
			}
			if fi.IsDir() {
				return errors.New("不是有效文件")
			}
			if err := os.Rename(old_path, new_path); err != nil {
				return err
			}
			cc++
		}
	}
	return nil
}
